package Threads.GeekBang_test;


//二阶段终止
public class test_35 {
    static volatile boolean isTerminated=false;
    static boolean started=false;
    static Thread WorkerThread;

    static void start(){
        if (started){
            return;
        }
        started=true;
        isTerminated=false;
        WorkerThread=new Thread(()->{
            while (!isTerminated){
                System.out.println("I am Working---");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
            started=false;
            System.out.println("i will be down");
        });
        WorkerThread.start();
    }

    static void stop(){
        isTerminated=true;
        WorkerThread.interrupt();
    }

    public static void main(String[] args) throws InterruptedException {
        test_35.start();
        Thread.sleep(5000);
        System.out.println("i will shutdown the work");
        test_35.stop();
    }
}
